import Taro from '@tarojs/taro'
import req from './req'
import axios from 'axios'
import S from '@/spx'

/**
 * @description: 获取上传Token
 * @param {params: 'image' or 'video' }
 */
// export const getOssToken = (params = {
//   filetype: 'image'
// }) => {
//   debugger
//   return fetch({
//     url: '/espier/oss_upload_token',
//     method: 'post',
//     data: params
//   })
// }

export function getOssToken(params) {
  return req.post('/espier/oss_upload_token', params)
}
export function LocalUpload(tokenRes, file, filetype = 'image') {
  // console.log(tokenRes)
  // console.log(file)
  const token = S.getAuthToken()
  return Taro.uploadFile({
    url: `${req.baseURL}espier/upload_localimage`,
    filePath: file.path,
    name: 'images',
    header: {
      'Authorization': `Bearer ${token}`
    },
    formData: {
      ...tokenRes,
      filetype
    }
  })
}

export const AliUpload = (tokenRes, file) => {
  // console.log('tokenRes', tokenRes, file)
  // const token = S.getAuthToken()
  // return Taro.uploadFile({
  //   url: tokenRes.host,
  //   name: 'images',
  //   filePath: file.path,
  //   header: {
  //     'Content-Type': 'multipart/form-data;charset=UTF-8' 
  //   },
  //   withCredentials: false,
  //   formData: {
  //     'key': tokenRes.dir,
  //     'policy': tokenRes.policy,
  //     'OSSAccessKeyId': tokenRes.accessid,
  //     'signature': tokenRes.signature,
  //     // name: file.name,
  //     'success_action_status': 200,
  //     'file': file.originalFileObj
  //   }
  // })
  const formData = new FormData()
  console.log(file,file.name,file.path)
  formData.append('key', tokenRes.dir)
  formData.append('policy', tokenRes.policy)
  formData.append('OSSAccessKeyId', tokenRes.accessid)
  formData.append('success_action_status', '200')
  // formData.append('callback', tokenRes.callback)
  formData.append('signature', tokenRes.signature)
  // formData.append('name', file.images)
  formData.append('file', file.originalFileObj)
  return axios({
    method: 'POST',
    url: tokenRes.host,
    headers: {  'Content-Type': 'multipart/form-data;charset=UTF-8' },
    data: formData
  })
}
